//
//  GMyMuseumVC.m
//  jingdongfang
//
//  Created by MTiMac02 on 2018/5/22.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "GMyMuseumVC.h"
#import "MyGArtCell.h"
#import "NoneFocusCell.h"
#import "HotRecomment.h"
#import "IGMyFocusMuseumApi.h"
#import "IGArtGalleryAttentApi.h"
#import "ArtGalleryVC.h"

@interface GMyMuseumVC ()
{
    NSString *uid;
    NSString *pageNum;//页数，从1开始
    NSString *type;
}
@property (nonatomic, strong)IGMyFocusMuseumApi *myMuseumApi;
@property (nonatomic, strong) IGArtGalleryAttentApi*attentApi;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)NSMutableArray *recDataArr;
@end

@implementation GMyMuseumVC
#pragma mark - 懒加载
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr=[NSMutableArray array];
    }
    return _dataArr;
}
-(NSMutableArray *)recDataArr{
    if (!_recDataArr) {
        _recDataArr=[NSMutableArray array];
    }
    return _recDataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor mainBackgroundColor];
    [self.tableView setTableFooterView:[[UIView alloc]initWithFrame:CGRectZero]];
    [self.tableView registerNib:[UINib nibWithNibName:@"MyGArtCell" bundle:nil] forCellReuseIdentifier:@"MyGArtCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"NoneFocusCell" bundle:nil] forCellReuseIdentifier:@"NoneFocusCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HotRecomment" bundle:nil] forCellReuseIdentifier:@"HotRecomment"];
     type=@"0";
        self.myMuseumApi=[IGMyFocusMuseumApi new];
        [self firstPage];
}
-(void)firstPage{
    pageNum= @"1";
    uid=[IGUser getUid];
    [self requestData];
}
-(void)requestData{
    self.myMuseumApi.pageNum=pageNum;
    self.myMuseumApi.uid=uid;
    @KWeakObj(self)
    [self.myMuseumApi startWithDecodeClass:[IGMyFocusMuseumResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            IGMyFocusMuseumResp *resp=responseObject;
            IGMyFocusMuseumModel *model=resp.data;
            if ([type integerValue]==1) {
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:2];
                [selfWeak.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            }else{
            if ([pageNum isEqualToString:@"1"])
            {
                _dataArr = [NSMutableArray arrayWithArray:model.focusMemberList];
                _recDataArr=[NSMutableArray arrayWithArray:model.recMemberList];
                
            }
            else
            {
                [_dataArr addObjectsFromArray:model.focusMemberList];
                [_recDataArr removeAllObjects];
                [_recDataArr addObjectsFromArray:model.recMemberList];
            }
            [selfWeak.tableView reloadData];
            }
        }
    }];
}
- (void)scrollToTOP
{
    [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (_dataArr.count!=0) {
        return 1;
    }
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        return _dataArr.count?_dataArr.count:0;
    }
    if (section==1){
    return 1;
    }
    if (section==2) {
         return _recDataArr.count?_recDataArr.count:0;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section==0) {
        MyGArtCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MyGArtCell" forIndexPath:indexPath];
        cell.data=_dataArr[indexPath.row];
        return cell;
    }
    if (indexPath.section==1){
        NoneFocusCell *cell=[tableView dequeueReusableCellWithIdentifier:@"NoneFocusCell" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.focusType=@"0";
        return cell;
    }
    if (indexPath.section==2) {
        HotRecomment *cell=[tableView dequeueReusableCellWithIdentifier:@"HotRecomment" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.data=_recDataArr[indexPath.row];
        
        [cell followOwerAction:^(MyFocusMemberModel *info) {
            self.attentApi = [[IGArtGalleryAttentApi alloc]init];
            _attentApi.operatorType = [info.attentionState boolValue]?@"0":@"1";
            _attentApi.museumId = info.aid;
            [self.attentApi startWithDecodeClass:[IGArtGalleryAttentResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
                if (responseObject) {
                } else {
                    if ([info.attentionState boolValue]) {
                        [self presentMessageTips:NSLocalizedString(@"Follow Failed", nil)];
                    } else {
                        [self presentMessageTips:NSLocalizedString(@"Cancel Failed", nil)];
                    }
                }
            }];
        }];
        [cell setNeedsLayout];
        return cell;
    }
    return nil;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        ArtGalleryVC *artVC = [[ArtGalleryVC alloc] init];
        MyFocusMemberModel *museum = _dataArr[indexPath.row];
        artVC.aId = museum.aid;
        [UIViewController ig_pushViewController:artVC animated:YES];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 71;
    }
    if (indexPath.section==1) {
        return 120;
    }
    if (indexPath.section==2) {
        return 70;
    }
    return 71;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 5;
    }
    if (section==1) {
        return 5;
    }
    if (section==2) {
        return 51;
    }    
    return 0.01f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section==1) {
        return 5;
    }
    return 0.01f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==2) {
        UIView *backView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 51)];
        backView.backgroundColor=[UIColor mainBackgroundColor];
        UIView *frontView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 50)];
        frontView.backgroundColor=[UIColor whiteColor];
        UILabel *hot=[[UILabel alloc]initWithFrame:CGRectZero];
        hot.text=@"热门艺术馆推荐";
        hot.font=[UIFont font16];
        hot.textColor=[UIColor c4Color];
        [hot sizeToFit];
        hot.x=KSCREENWIDTH/2-hot.width/2;
        hot.centerY=frontView.centerY;
        [frontView addSubview:hot];
        UIButton *changeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        changeBtn.frame=CGRectMake(0, 0, 50, 30);
        [changeBtn setTitle:@"换一批" forState:UIControlStateNormal];
        [changeBtn setTitle:@"换一批" forState:UIControlStateSelected];
        [changeBtn setImage:[UIImage imageNamed:@"gexchange"] forState:UIControlStateNormal];
        [changeBtn setImage:[UIImage imageNamed:@"gexchange"] forState:UIControlStateSelected];
        [changeBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
        [changeBtn setTitleColor:[UIColor c5Color] forState:UIControlStateNormal];
        [changeBtn setTitleColor:[UIColor c5Color] forState:UIControlStateSelected];
        [changeBtn sizeToFit];
        changeBtn.centerY=frontView.centerY;
        changeBtn.right=frontView.right-12;
        
        [changeBtn addTarget:self action:@selector(changeBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [frontView addSubview:changeBtn];
        [backView addSubview:frontView];
        return backView;
    }
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section==1) {
        UIView *footer=[[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 5)];
        return footer;
    }
    return nil;
}

-(void)changeBtnClick{
    pageNum = [NSString stringWithFormat:@"%ld", [pageNum integerValue] + 1];
    [self requestData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
